/*
MasterRollUp
Version 1.0
Description: Class for Computing field prefixed 'RU' in a Master Account from children accounts 'RollUp Summary' type fields.
Author: Pratibh Prakash
Created Date: 4 Mar-2009
*/
public class MasterRollUp {
	/*
	rollUp()
	@param accounts: Contains list of Account records as sent by trigger.
	Description: Updates Account record fields prefixed RU.
	*/
	public static void rollUp(Set<String> accounts){
		 
		List<Account> forProcessing = new List<Account>();  
      	List<Account> forUpdate = new List<Account>();      
	    String lastCode = '';
    	Account masterAcct = null;
	    Double plActualYtdTotal = 0;
	    Double clActualYtdTotal = 0;
    	Double plRolling12WPTotal = 0;
      	Double clRolling12WPTotal = 0;
      	Double totalRolling12WPTotal = 0;
      	Double plActualPYTotal = 0;
      	Double clActualPYTotal = 0;
      	
      	
      	Double clVarianceYTDTotal = 0;
      	Double totalVarianceYTDTotal = 0;  
      
      	// 8-14-2008 - wng - added new rollup fields
		if (accounts.size() > 0) {
         //forProcessing = ;
        	
			for(Account acct: [SELECT PL_Actual_YTD__c, CL_Actual_YTD__c, PL_Rolling_12_WP__c, 
         					  CL_Rolling_12_WP__c, PL_Actual_PY__c, CL_Actual_PY__c,
                         	  CL_Variance_YTD__c, Master_Agency_Code__c, AccountNumber
                         	  FROM Account 
                         	  WHERE Master_Agency_Code__c in :accounts 
                         	  Order By Master_Agency_Code__c]){
            	
            	if ((lastCode == '') || (lastCode == acct.Master_Agency_Code__c)) {
               		plActualYtdTotal += acct.PL_Actual_YTD__c;
            		clActualYtdTotal += acct.CL_Actual_YTD__c;
		            plRolling12WPTotal += acct.PL_Rolling_12_WP__c;
	            	clRolling12WPTotal += acct.CL_Rolling_12_WP__c;
	            	plActualPYTotal += acct.PL_Actual_PY__c;
            		clActualPYTotal += acct.CL_Actual_PY__c;
            		clVarianceYTDTotal += acct.CL_Variance_YTD__c;
		              
            		// 8-14-2008 - wng - added new rollup fields
            		if (lastCode == '') {
	              		lastCode = acct.Master_Agency_Code__c;
            		}
	            
    	        	//check if the acct is the master agency
        	    	if (acct.Master_Agency_Code__c == acct.AccountNumber) {
	            		masterAcct = acct.clone(true);
            		}
          		}
	        }
	        
	        if (masterAcct != null) {
          		masterAcct.RU_PL_Actual_YTD__c = plActualYtdTotal;
          		masterAcct.RU_CL_Actual_YTD__c = clActualYtdTotal;
		        masterAcct.RU_PL_Rolling_12_WP__c = plRolling12WPTotal;
        		masterAcct.RU_CL_Rolling_12_WP__c = clRolling12WPTotal;
          		masterAcct.RU_PL_Actual_PY__c = plActualPYTotal;
          		masterAcct.RU_CL_Actual_PY__c = clActualPYTotal;
          		masterAcct.RU_CL_Variance_YTD__c = clVarianceYTDTotal;
          		forUpdate.add(masterAcct);
        	}           

        	if (forUpdate.size() > 0) {
          		update forUpdate;
        	}
		}
	}
}